// ================================================================================
// 		File: PartyVolunteerActivitie.cs
// 		Desc: 
//  
// 		Called by:   
//               
// 		Auth: chenwenzhi（）
// 		Date: 2016-04-01 22:01
// ================================================================================
// 		Change History
// ================================================================================
// 		Date:		Author:				Description:
// 		--------	--------			-------------------
//    
// ================================================================================
// 
// ================================================================================
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace ZHDJ.Data.Models.Center
{
    /// <summary>
    /// 志愿活动
    /// </summary>   
    public class PartyVolunteerActivitie : EntityBase
    {
        #region 属性


        /// <summary>
        /// 组织ID
        /// </summary>
        public string BranchID { get; set; }

        /// <summary>
        /// 活动标题
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// 所属专题
        /// </summary>
        public string Thematic { get; set; }

        /// <summary>
        /// 活动类型
        /// </summary>
        public string Category { get; set; }

        /// <summary>
        /// 活动召集通知
        /// </summary>
        public string Summary { get; set; }

        /// <summary>
        /// 活动开始时间
        /// </summary>
        public DateTime? BeginTime { get; set; }

        /// <summary>
        /// 活动结束时间
        /// </summary>
        public DateTime? EndTime { get; set; }

        /// <summary>
        /// 服务时长（小时）
        /// </summary>
        public int ServiceTime { get; set; }

        /// <summary>
        /// 活动地点
        /// </summary>
        public string Address { get; set; }

        /// <summary>
        /// 地图坐标
        /// </summary>
        public string Coordinate { get; set; }

        /// <summary>
        /// 短信通知
        /// </summary>
        public string IsSmsAlert { get; set; }

        /// <summary>
        /// 短信通知内容
        /// </summary>
        public string SmsContent { get; set; }

        /// <summary>
        /// 附件
        /// </summary>
        public string FileID { get; set; }

        /// <summary>
        /// 图片ID
        /// </summary>
        public string ImageID { get; set; }

        /// <summary>
        /// 是否结束
        /// </summary>
        public bool? IsEnd { get; set; }

        /// <summary>
        /// 活动记录
        /// </summary>
        public string Record { get; set; }

        /// <summary>
        /// 0、无；1、置顶；2、固顶；3、置顶+固顶；
        /// </summary>
        public int Flags { get; set; }

        /// <summary>
        /// 发布状态( 1 草稿  4发布 )
        /// </summary>
        public int Status { get; set; }

        /// <summary>
        /// 首页显示
        /// </summary>
        public bool IsShowHome { get; set; }

        /// <summary>
        /// 首页显审核状态(1审核中 2驳回  4审核通过)
        /// </summary>
        public int ShowHomeAduit { get; set; }

        /// <summary>
        /// 首页显示审核人
        /// </summary>
        public string ShowHomeAduitBy { get; set; }

        /// <summary>
        /// 首页显示审核时间
        /// </summary>
        public DateTime? ShowHomeAduitTime { get; set; }

        /// <summary>
        /// 是否已推送到上级
        /// </summary>
        public string IsPushUp { get; set; }

        /// <summary>
        /// 推送审核状态(1审核中 2驳回  4审核通过)
        /// </summary>
        public int Aduit { get; set; }

        /// <summary>
        /// 推送审核人
        /// </summary>
        public string AduitBy { get; set; }

        /// <summary>
        /// 推送审核时间
        /// </summary>
        public DateTime? AduitTime { get; set; }

        /// <summary>
        /// 源ID
        /// </summary>
        public string RelationID { get; set; }

        /// <summary>
        /// 修改用户
        /// </summary>
        public string ModifyBy { get; set; }

        /// <summary>
        /// 创建用户
        /// </summary>
        public string CreateBy { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime? CreateTime { get; set; }

        /// <summary>
        /// 更新时间
        /// </summary>
        public DateTime? ModifyTime { get; set; }

        #endregion

        #region 查询管理
        public static DataSet GetGridData(int pageIndex, int pageSize, string title, string branchId, string isShowHome)
        {
            List<MySqlParameter> parameters = new List<MySqlParameter>()
            {
                new MySqlParameter(){DbType = DbType.Int32,ParameterName = "Begin",Value = (pageIndex)*pageSize},
                new MySqlParameter(){DbType = DbType.Int32,ParameterName = "PageSize",Value = pageSize}
            };


            StringBuilder sb = new StringBuilder();
            sb.Append(@"select SQL_CALC_FOUND_ROWS  a.ID,a.Title,BeginTime,EndTime,if(IsEnd=1,'是','否') as IsEndName,UserName, if(Status=4,'是','否') as StatusName,if(Flags=1,'是','否') as FlagsName from tblpartyvolunteeractivitie a left join tblpartyorgmember m on a.CreateBy=m.LoginID where 1=1 ");

            if (!string.IsNullOrEmpty(title))
            {
                sb.Append(" and a.Title like ?title ");
                parameters.Add(
                    new MySqlParameter() { DbType = DbType.String, ParameterName = "Title", Value = string.Format("%{0}%", title) });
            }
            if (!string.IsNullOrEmpty(branchId))
            {
                sb.Append(" and a.BranchID=?BranchID ");
                parameters.Add(
                    new MySqlParameter() { DbType = DbType.String, ParameterName = "BranchID", Value = string.Format("{0}", branchId) });
            }
            if (!string.IsNullOrEmpty(isShowHome))
            {
                sb.Append(" and IsShowHome=?isShowHome ");
                parameters.Add(
                    new MySqlParameter() { DbType = DbType.String, ParameterName = "IsShowHome", Value = string.Format("{0}", isShowHome) });
            }

            sb.Append(" order by a.CreateTime desc LIMIT ?Begin ,?PageSize;SELECT FOUND_ROWS();");
            return MySqlHelper.ExecuteDataSet(sb.ToString(), parameters.ToArray());
        }

        public static DataSet GetPersonalGridData(int pageIndex, int pageSize, string title, string createBy, string branchId, string type)
        {
            List<MySqlParameter> parameters = new List<MySqlParameter>()
            {
                new MySqlParameter(){DbType = DbType.Int32,ParameterName = "Begin",Value = (pageIndex)*pageSize},
                new MySqlParameter(){DbType = DbType.Int32,ParameterName = "PageSize",Value = pageSize}
            };


            StringBuilder sb = new StringBuilder();
            sb.Append(@"select SQL_CALC_FOUND_ROWS  a.ID,a.Title,BeginTime,EndTime,if(IsEnd=1,'是','否') as IsEndName,UserName,CONCAT(ServiceTime,'小时') as ServiceTime
                        from tblpartyvolunteeractivitie a
                        left join tblpartyorgmember m on a.CreateBy=m.LoginID
                        where 1=1 ");

            if (!string.IsNullOrEmpty(title))
            {
                sb.Append(" and Title like ?Title ");
                parameters.Add(
                    new MySqlParameter() { DbType = DbType.String, ParameterName = "Title", Value = string.Format("%{0}%", title) });
            }
            if (type == "0")
            {
                sb.Append(" and a.BranchID=?BranchID ");
                parameters.Add(
                    new MySqlParameter() { DbType = DbType.String, ParameterName = "BranchID", Value = string.Format("{0}", branchId) });
            }
            else
            {
                sb.Append(" and a.ID in (select ActivitieID from tblpartyvolunteeractivitiemember where LoginID=?LoginID)");
                parameters.Add(
                    new MySqlParameter() { DbType = DbType.String, ParameterName = "LoginID", Value = string.Format("{0}", createBy) });
            }

            sb.Append(" order by a.CreateTime desc LIMIT ?Begin ,?PageSize;SELECT FOUND_ROWS();");
            return MySqlHelper.ExecuteDataSet(sb.ToString(), parameters.ToArray());
        }

        public static DataSet GetActiviteTitle(string branchId, string countFilter = "")
        {
            StringBuilder sb = new StringBuilder();
            List<MySqlParameter> list = new List<MySqlParameter>();
            sb.Append("select ID,Title,IsEnd,CASE IsEnd WHEN 0 THEN '进行中' WHEN 1 THEN '已结束' WHEN 2 THEN '未开始' END as IsEndName from tblpartyvolunteeractivitie where Status=2");
            if (!string.IsNullOrEmpty(branchId))
            {
                sb.Append(" and BranchID=?BranchID");
                list.Add(new MySqlParameter() { DbType = DbType.String, ParameterName = "BranchID", Value = branchId });
            }
            sb.AppendFormat(" order by Flags desc,CreateTime desc {0};SELECT FOUND_ROWS();", countFilter);

            return MySqlHelper.ExecuteDataSet(sb.ToString(), list.ToArray());
        }

        public static DataSet GetMoreActivitieTitle(int pageSize, int pageIndex, string branchId = "")
        {
         
            StringBuilder sb = new StringBuilder();
            List<MySqlParameter> parameters = new List<MySqlParameter>();
            
            sb.Append(@"select SQL_CALC_FOUND_ROWS ID,Title,CASE IsEnd WHEN 0 THEN '进行中' WHEN 1 THEN '已结束' WHEN 2 THEN '未开始' END as IsEndName,CreateTime 
                        from tblpartyvolunteeractivitie where Status=2  ");
            if (!string.IsNullOrEmpty(branchId))
            {
                sb.Append(" and BranchID=?BranchID");
                parameters.Add(new MySqlParameter() { DbType = DbType.String, ParameterName = "BranchID", Value = branchId });
            }

            sb.Append(" order by CreateTime desc ");
            if (pageIndex > -1)
            {
                sb.AppendFormat(" LIMIT {0} ,{1}; SELECT FOUND_ROWS(); ", pageIndex, pageSize);
            }

            return MySqlHelper.ExecuteDataSet(sb.ToString(), parameters.ToArray());
        }

        public static int GetPeopleCountByActivityID(string activityID)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select count(*) from tblpartyvolunteeractivitiemember where ActivitieID='" + activityID + "'");

            return Convert.ToInt32(MySqlHelper.ExecuteScalar(sb.ToString()));
        }
        #endregion
    }
}
